<?php
namespace app\index\controller;
use app\common\controller\Base;
use think\Db;

class Bonus extends Base
{
    
    
    public function _initialize() {
       
        
        parent::_initialize();
    }
    
    public function raffle(){
        $refer = input('server.HTTP_REFERER');
        if(!isset($refer) || !stripos($refer,$_SERVER['HTTP_HOST'])){
            return json(array('msg'=>'非法操作！','status'=>0));
        }
        
        if(empty($this->user_all['username'])){
            return json(array('msg'=>'请先登录！','status'=>0));
        }
        
        if($this->user_all['swzh'] == 1){
            return json(array('msg'=>'试玩账号无法参与抢红包！','status'=>0));
        }
        //查询红包开启时间和开启类型
        $on_lay = Db::name('bonus_lay')->find();
        if($on_lay['flip'] == 0){
            return json(array('msg'=>'红包暂未开启！','status'=>0));
        }
        
        //查询是否到开奖时间
        if(strtotime($on_lay['start_time']) < time() && strtotime($on_lay['end_time']) > time()){
            
            //获取用户抢红包次数
            $sum = 0;
            $lay_sum = Db::name('bonus_num')->order('saving asc')->select();
            
            foreach ($lay_sum as $s){
                if($this->user_all['czzje'] >= $s['saving']){
                    $sum = $s['num'];
                }else{
                    break;
                }
            }
            
            $user_sum = Db::name('bonus_user')->where('username',$this->user_all['username'])->whereTime('time', 'd')->field('id')->count();
            $sum -= $user_sum;
            
            if($sum <= 0){
                return json(array('msg'=>'您的抢红包次数不足','status'=>0));
            }
            $lv = 0;
            
            //判断是哪种红包类型
            if($on_lay['flip'] == 1){
                
                $bonus_lv = Db::name('bonus')->order('lv asc')->select();
                
                
                foreach ($bonus_lv as $b){
                    $cz = Db::name('chongzhi_list')->whereTime('cztime','today')->where('username',$this->user_all['username'])->value('SUM(je)');
                    if($cz >= $b['min_saving']){
                        
                        if($cz <= $b['max_saving']){
                            $lv = $b['lv'];
                        }else{
                            continue;
                        }
                        
                    }else{
                        break;
                    }
                    
                }
                
                if($lv == 0){
                    return json(array('msg'=>'您的充值金额不满足当前抢红包的条件','status'=>0));
                }
                
                $bonus_chance = Db::name('bonus_chance')->where('lv',$lv)->column('chance,min_money,max_money');
                
                $win = getWin($bonus_chance,'chance');
                if(count($bonus_chance)<1){
                    return json(array('msg'=>'您的存款金额不满足抢红包条件','status'=>0));
                }
                $min_money = $bonus_chance[$win]['min_money'];
                $max_money = $bonus_chance[$win]['max_money'];
                
                $money= mt_rand($min_money*100,$max_money*100)/100;
                
                $new_money  = sprintf("%.2f",$money);
                
                
                
            }else if($on_lay['flip'] == 2){
                $bonus_chance = mt_rand(1, 100);
                $win = Db::name('bonus_chance')->where(array('lv'=>$this->user_all['us_class'],'type'=>2))->field('chance,min_money,max_money,chance_money')->find();
                
                if(empty($win)){
                    return json(array('msg'=>'您所在的等级暂时无法抢红包','status'=>0));
                }
                if($bonus_chance <= $win['chance']){
                    $new_money = $win['chance_money'];
                }else{
                    $money= mt_rand($win['min_money']*100,$win['max_money']*100)/100;
                    
                    $new_money  = sprintf("%.2f",$money);
                }
                $lv = $this->user_all['us_class'];
            }
            if($new_money){
                Db::startTrans();
                
                try{
                    $data = array();
                    $data['username'] = $this->user_all['username'];
                    $data['je'] = $new_money;
                    $data['lv'] = $lv;
                    $data['type'] = $on_lay['flip'];
                    $data['time'] = time();
                    Db::name('bonus_user')->insert($data);
                    Db::name('username')->where('username', $this->user_all['username'])->setInc('je', $new_money);
                    Db::commit();
                }catch (\Exception $e) {
                    Db::rollback();
                }
                return json(array('msg'=>'恭喜您抢到'.$new_money.'元，剩余抢红包次数'.($sum-1).'次','status'=>1));
            }
            
        }else{
            return json(array('msg'=>'未到开奖时间<br/>开奖时间为'.$on_lay['start_time'].'-'.$on_lay['end_time'],'status'=>0));
        }
        
        
        
        
    }
}